package interfaces.spawnMenu.costumize;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import fileHandling.language.Lang;

import logic.common.player.Player;
import logic.nodes.nodeSettings.Settings;
import logic.nodes.nodeSettings.upgrades.UpgradableSettings;

import interfaces.PropertyContent.Mode;
import interfaces.PropertyWidget;
import interfaces.superWidgets.MoneyListener;
import interfaces.superWidgets.StaticContent;

public abstract class InfoContent extends StaticContent implements MoneyListener {
	
	protected int infoWidgetHeight, widgetY;
	protected List<PropertyWidget> allWidgets;
	protected boolean upgradesInitialized;
	protected Settings maxValues;
	protected HashMap<Lang, String[]> keys;
	protected List<Lang> keyOrderList;
	protected Mode mode;
	protected UpgradableSettings settings;
	protected Player player;
	
	public InfoContent(int width, int height, int x, int y, HashMap<Lang, String[]> keys, 
			UpgradableSettings settings, Settings maxValues, Mode propMode, 
			boolean init, int maxItems, List<Lang> keyOrderList, Player player) {
		super(width, height, x, y);
		this.maxValues = maxValues;
		this.keys = keys;
		this.settings = settings;
		this.mode = propMode;
		this.keyOrderList = keyOrderList;
		this.player = player;
		
		if(init) init(maxItems);
	}
	
	public InfoContent(int width, int height, int x, int y, HashMap<Lang, String[]> keys, 
			UpgradableSettings settings, Settings maxValues, Mode propMode, 
			boolean init, List<Lang> keyOrderList, Player player) {
		this(width, height, x, y, keys, settings, maxValues, propMode, init, keys.size(), keyOrderList, player);
	}
	
	public InfoContent(int width, int height, int x, int y, HashMap<Lang, String[]> keys, 
			UpgradableSettings settings, Settings maxValues, Mode propMode, 
			List<Lang> keyOrderList, Player player) {
		this(width, height, x, y, keys, settings, maxValues, propMode, true, keyOrderList, player);
	}
	
	protected void init(int keySize) {
		allWidgets = new ArrayList<PropertyWidget>();
		
		infoWidgetHeight = height / keySize;
		widgetY = height - infoWidgetHeight;
		
		createWidgets();
	}
	
	public void initUpgrades(boolean init) {
		if(upgradesInitialized == init) return;
		for(PropertyWidget widget : allWidgets) {
			widget.initUpgrade(init);
		}
		upgradesInitialized = init;
	}
	
	protected void createWidgets() {
		for(Lang descr : keyOrderList) {
			addWidget(descr, keys.get(descr));
		}
	}
	
	protected PropertyWidget addWidget(Lang descr, String[] keys) {
		PropertyWidget widget = null;
		if(mode != null) 
			widget = new PropertyWidget(descr, width, infoWidgetHeight, settings, keys, maxValues, mode, player);
		else widget = new PropertyWidget(descr, width, infoWidgetHeight, settings, keys, maxValues, player);
		widget.setXY(0, widgetY);
		addWidget(widget);
		allWidgets.add(widget);
		widgetY -= infoWidgetHeight;
		return widget;
	}
	
	public void setProperties(UpgradableSettings props) {
		settings = props;
		for(PropertyWidget prop : allWidgets) {
			prop.setProperties(settings);
		}
	}
	
	public List<PropertyWidget> getAllWidgets() { return allWidgets; }
	
	@Override
	public void updateMoney(int newMoney) {
		for(PropertyWidget widget : allWidgets) {
			widget.updateMoney(newMoney);
		}
	}
}